<?php
namespace Admin\Controller;
use Think\Controller;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/25
 * Time: 14:49
 */
class TypeController extends CommonController
{
    private $model;
    public function __construct()
    {
        parent::__construct();
        $this->model = D('Type');
    }

    /**
    * 类型列表
    * @author wang
    * @param 
    */
    public function typeList()
    {   
        $where = array();
        $sort = I('request.sort', 'orderid');
        $order = I('request.order', 'asc');
        $type = I('type');

        if(!empty($type)) $where['type|description'] = array('like','%'.$type.'%');
        $result = $this->model->getList($where,$sort." ".$order);
        foreach ($result as $k => &$v) {
            $v['createtime']=date("Y-m-d H:i:s",$v['createtime']);
            $v['updatetime']=date("Y-m-d H:i:s",$v['updatetime']);
            $v['orderid']=(int)$v['orderid'];
        }
        $this->ajaxReturn($result);
    }

    /**
    * 增加类型
    * @author wang
    * @param 
    */
    public function addType(){

    	$data = array(
    		'type' => I('type'),
    		'description' => I('description'),
            'orderid' => I('orderid'),
            'typecode' => I('typecode'),
            'admin' => session('admin'),
    		'updatetime' => time(),
    	);
        $icon = $this->uploadImg();
        $data['icon'] = implode(" ",$icon['icon']);

        // $data['id'] = (new  \Org\Twitter\IdWork(31,31))->nextId();
        $data['createtime']  = time();
		if($this->model->add($data)){
        	$this->ajaxReturn(array('success' => 1, 'msg' => '用户新增成功'));
        }else{
            $this->ajaxReturn(array('success' => 0, 'msg' => '用户增加失败'));
        }

    }

    /**
    * 删除类型
    * @author wang
    * @param 
    */
    public function delType(){
        $typeid = I('typeid');
        if($this->model->where("typeid='$typeid'")->delete()){
            $result = array(
                'sta' => 1, 
                'msg' => '删除成功'
            );
        }else{
            $result = array(
                'sta' => 0, 
                'msg' => '删除失败'
            ); 
        }
        $this->ajaxReturn($result);
    }



/**
  * 查看atype详情
  * @author wang
  * @param 
  */
  public function typeDetial(){
    $typeid = I('typeid');
    $data = M('type')
            ->where("typeid='$typeid'")
            ->find();
    $data['orderid']=(int)$data['orderid'];
    $data['createtime']=date("Y-m-d H:i:s",$data['createtime']);
    $data['updatetime']=date("Y-m-d H:i:s",$data['updatetime']);
    $this->assign("data",$data);
    $this->display('detial');
  }



    /**
  * 修改内容
  * @author wang
  * @param 
  */
  public function editImg(){
    $data = I();
    $data['admin'] = session('admin');
    $data['updatetime'] = time();
    $typeid = $data['typeid'];
    $icon = $this->uploadImg();
    if($icon){
       $data['icon'] = implode(" ",$icon['icon']); 
    }
    if(M('type')->where("typeid='$typeid'")->save($data)){
        $this->ajaxReturn(1);
    }else{
        $this->ajaxReturn(0);
    }
  }


}